
rm(list = ls())

library("data.table")  
library("ggplot2")  
library("ggrepel")  
library("stargazer")  


# Import Data
#===============================================================================

gft <- fread('gft_full_adj_agg.csv')
gft[ , diff := gft_20 - gft_80]

c_gamma <- fread('share_service_20_80.csv')
colnames(c_gamma) <- c("declarant", "gamma_20", "gamma_80")
c_gamma[declarant == 92, declarant := 91]


# Construct Overall Price Index
#===============================================================================

gft <- merge(gft, c_gamma, by = "declarant")
gft[ , gft_20_adj := (gft_20 - 1) * (1 - gamma_20)]
gft[ , gft_80_adj := (gft_80 - 1) * (1 - gamma_80)]
gft[ , diff_adj := gft_20_adj - gft_80_adj]
diff_save <- gft[ , c(3,4,5,13)]
write.csv(file = 'diff_full_adj_overall.csv', diff_save)

# Plot
#===============================================================================

iso3 <- fread('ISO3_declarant_codes.csv')

gft[ , gdp_log := log(gdp_per_capita_declarant)]
setkey(gft, declarant)
setkey(iso3, declarant)

gft <- gft[iso3]
gft <- gft[is.na(country) == F]
gft = gft[declarant != 18] 
gft = gft[declarant != 46] 
gft = gft[declarant != 600] 

reg <- lm(diff_adj ~ gdp_log, data = gft)
summary(reg)
cor(gft$diff_adj, gft$gdp_log)

plot_diff_full_adj_overall <- ggplot(gft, aes(x=gdp_log, y=diff_adj)) +
                geom_text_repel(label=gft$ISO3, size = 5) +
                theme(text = element_text(size=17)) +
                geom_smooth(method=lm) +
                labs(x = "GDP per capita (in logs)", y = expression(paste(Delta, "Welfare"["Poor"]," - ", Delta, "Welfare"["Rich"]))) 
                
ggsave("plot_diff_full_adj_overall.png", plot = plot_diff_full_adj_overall)


